﻿namespace ManagementApi.Models
{
    public class CardInfo
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex { get; set; }
        /// <summary>
        /// 民族
        /// </summary>
        public string Race { get; set; }
        /// <summary>
        /// 出生日期
        /// </summary>
        public string Birth { get; set; }
        /// <summary>
        /// 居住地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 公民身份证号
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// 签发机关
        /// </summary>
        public string Company { get; set; }
        /// <summary>
        /// 有效起始日期
        /// </summary>
        public string BeginDate { get; set; }
        /// <summary>
        /// 有效截止日期
        /// </summary>
        public string EndDate { get; set; }
        /// <summary>
        /// 照片
        /// </summary>
        public string HeadPic { get; set; }

        /// <summary>
        /// 读取身份证时是否包含安全设备信息
        /// </summary>
        public bool HasDeviceInfo { get; set; }

        public byte[] GetHeadPicData()
        {
            if (string.IsNullOrEmpty(HeadPic))
                return null;
            return Convert.FromBase64String(HeadPic);
        }

        public DateTime? GetBeginDate()
        {
            if (!string.IsNullOrEmpty(BeginDate) && BeginDate.Length >= 8)
            {
                string year = BeginDate.Substring(0, 4);
                string month = BeginDate.Substring(4, 2);
                string day = BeginDate.Substring(6, 2);
                return DateTime.TryParse($"{year}-{month}-{day}", out DateTime beginDate) ? beginDate : null;
            }
            return null;
        }

        public DateTime? GetEndDate()
        {
            if (!string.IsNullOrEmpty(EndDate) && EndDate.Length >= 8)
            {
                string year = EndDate.Substring(0, 4);
                string month = EndDate.Substring(4, 2);
                string day = EndDate.Substring(6, 2);
                return DateTime.TryParse($"{year}-{month}-{day}", out DateTime endDate) ? endDate : null;
            }
            return null;
        }
    }
}
